﻿// echo partial file
#if FRONTEND
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Config;
using GameData.Domains.Character;
using GameData.Domains.Character.Display;
using static NpcScan.Const;
using Newtonsoft.Json;
namespace NpcScan
{
    public class CharacterDataById: IComparer<CharacterData> {
        public static CharacterDataById Instance=new CharacterDataById();
        public int Compare(CharacterData x,CharacterData y)=>x.id.CompareTo(y.id);
    }
    public partial class CharacterData
    {
        // public string name;
        [JsonIgnore] public int isAlive;
        [JsonIgnore] public short zhoutian_yunzhuan;
        [JsonIgnore] public bool transgender;
        [JsonIgnore] public bool bisexual;
        [JsonIgnore] public string organization;
        [JsonIgnore] public string identify;
        [JsonIgnore] public string[] preexistenceCharacterNames;
        [JsonIgnore] public string[] itemList;
        [JsonIgnore] public string[] featureList;

        public void Fill(){
            // MainEntry.logger($"receive {this.id}");
            isAlive=gender>>24;
            transgender=(gender&(1<<16))>0;
            bisexual=(gender&(1<<8))>0;
            zhoutian_yunzhuan=(short)(ideal>>16);
            ideal&=65535;
            OrganizationInfo organizationInfo = new OrganizationInfo((sbyte)this.organizationInfo[0], (sbyte)this.organizationInfo[1], Convert.ToBoolean(this.organizationInfo[2]), (short)this.organizationInfo[3]);
            short randomNameId = (short)(Utils.map.SettlementRandNameDict.ContainsKey(organizationInfo.SettlementId) ? Utils.map.SettlementRandNameDict[organizationInfo.SettlementId] : -1);
            this.organization = CommonUtils.GetOrganizationString(organizationInfo.OrgTemplateId, randomNameId);
            this.identify = CommonUtils.GetCharacterGradeString(organizationInfo, (sbyte)this.gender);

            // this.name = $"id: <color={Colors.Instance.GradeColors[this.organizationInfo[1]].ColorToHexString()}>{this.id}</color>\n{CharacterData.GetName(this.Name)}";

            // this.itemList = new List<string>();
            // foreach (int item in this.items)
            // {
            //     // int itemType = item[0];
            //     // int itemId = item[1];
            //     // int itemGrade = item[2];
            //     // string name = Utils.GetItemName(item);
            //     this.itemList.Add($"<color={Colors.Instance.GradeColors[item>>item_grade_shift].ColorToHexString()}>{Utils.GetItemName(item)}</color>");
            // }
            this.itemList=(from item in this.items select Utils.GetItemName(item)).ToArray();
            this.featureList = (from feature in this.featureIds.Concat(this.potentialFeatureIds) select CharacterFeature.Instance[feature].Name).ToArray();
            // this.featureList = new List<string>(this.featureIds.Length+this.potentialFeatureIds.Length);
            // this.featureIds.ForEach(feature => this.featureList.Add(CharacterFeature.Instance[feature].Name));
            // this.potentialFeatureIds.RemoveAll(feature => this.featureIds.Contains(feature));
            // this.potentialFeatureIds.ForEach(feature => this.featureList.Add(CharacterFeature.Instance[feature].Name));
            // this.preexistenceCharacterNames=(from name in this.samsara select GetName(name)).ToArray();
            this.preexistenceCharacterNames=(from id in this.samsara select NMdict[id]).ToArray();
        }
        // public unsafe static string GetName(byte[] Name){
        //     NameRelatedData nameRelatedData=new NameRelatedData();
        //     fixed(byte*x=Name){nameRelatedData.Deserialize(x);}
        //     var display_name=nameRelatedData.GetMonasticTitleOrDisplayName(false);
        //     var real_name=nameRelatedData.GetRealName();
        //     if(display_name!=real_name) return $"{real_name.Item1+real_name.Item2}\n({display_name.Item1+display_name.Item2})";
        //     return $"{real_name.Item1+real_name.Item2}";
        // }
    }
}
#endif
